//
//  ViewController.m
//  21---NSURLRequestAndResponse
//
//  Created by 冀永金 on 16/8/11.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   // NSURL 负责请求地址
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/user/wanghuang/1.html?userName=xxx&pwd=xxx"];
    NSLog(@"baidu:%@",url);
    // 完整的URL对应的字符串
    NSLog(@"badidu.absoluteString:%@",url.absoluteString);
    // host：主机地址/ 域名
    NSLog(@"baidu.host:%@",url.host);
    
    NSLog(@"baidu.path:%@",url.path);
    // query: ? 后面的内容
    NSLog(@"badu.query:%@",url.query);
    // 不可变请求 和可变请求
    //NSURLRequest
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 不可改变的啥都不能改
    //NSMutableURLRequest
    NSMutableURLRequest *muatbleUrlRequest = [NSMutableURLRequest requestWithURL:url];
    // 超时时间 一个请求持续多久 会失败
    muatbleUrlRequest.timeoutInterval = 40;
    // HttpMethod 请求的姿势  GET POST PUT DELETE
    muatbleUrlRequest.HTTPMethod = @"GET";
    //allHTTPHeaderFields 设置网络请求头
    muatbleUrlRequest.allHTTPHeaderFields = @{@"USer":@"iOS10"};
    // 根据字典特性， 相同Key情况下，后填入的值会覆盖前面的值
    [muatbleUrlRequest setValue:@"iOS10" forHTTPHeaderField:@"User"];
    NSLog(@"%@",muatbleUrlRequest.allHTTPHeaderFields);
    // HTTPBody(一般POST请求时，才会使用)
    // 一般情况下，都是字典/字符串转换成的NSData
    muatbleUrlRequest.HTTPBody = [NSData data];
    
    
    // 3.NSURLResponse 相应
    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc]initWithURL:url statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:@{@"data":[NSData data]}];
    //1.statusCode 状态码
    //response.statusCode
    // 2.allHeaderFields 响应头
    //Content---- Type:Application/json;charset:utf-8
    // 时间 日期
    // 缓存一些内容
    NSLog(@"%@",response.allHeaderFields);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
